/*
xp_wdg.c

offer watch dog init and feed dog funtion
designed by lunanting


-----------------------------
2021-03-09 14:58:57
create file
-----------------------------

*/

#include "xp_wdg.h"
#include "cli_cmd.h"


extern int xp_wdg_mcu_init(int ot);
void xp_wdg_feed(void *arg1,void *arg2);
void xp_wdg_hal_stop(void);
void xp_wdg_hal_open(void);
static int xp_wdg_debug(char *type,char *fun,char *param);

static WDG_data wdg;



/*
init watch dog
param:
    ot:over time s
    ft:feed time s
return:
    0=success
*/
int xp_wdg_init(int ot,int ft){
    if(ot<1||ot>127){
        perr("over time set fail ~,ot = %d .",ot);
        return -1;
    }
    aos_timer_new(&wdg.timer,xp_wdg_feed,NULL,ft*1000,1);
    if(xp_wdg_mcu_init(ot*2)!=0)return -2;
    wdg.dogPort=1;
    wdg.timeout=ot;
    wdg.feed_cycle=ft;
    xp_cli_debug_add(xp_wdg_debug);
    return 0;
}


/*
init watch dog
param:
    ot:over time s
    ft:feed time ms
return:
    0=success
*/
int xp_wdg_init_ms(int ot,int ft_ms){
    if(ot<1||ot>127){
        perr("over time set fail ~,ot = %d .",ot);
        return -1;
    }
    aos_timer_new(&wdg.timer,xp_wdg_feed,NULL,ft_ms,1);
    if(xp_wdg_mcu_init(ot)!=0)return -2;
    wdg.dogPort=1;
    wdg.timeout=ot;
    wdg.feed_cycle=ft_ms;
    xp_cli_debug_add(xp_wdg_debug);
    return 0;
}

/*
stop watch dog
*/
void xp_wdg_stop(void){
    aos_timer_stop(&wdg.timer);
    aos_msleep(10);
    xp_wdg_hal_stop();
    aos_msleep(10);
}


/*
open watch dog
*/
void xp_wdg_open(void){
    xp_wdg_hal_open();
}


/**************************ok1052 core ********************************/
#if defined OK1054W || defined OK1053W || defined OK1052C || defined OK1053A
#include "fsl_wdog.h"
/*
init watch dog
param:
    ot:over time
return:
    0=success
*/
int xp_wdg_mcu_init(int ot){
    wdog_config_t config;
    WDOG_GetDefaultConfig(&config);
    config.timeoutValue=ot;
    // config.enableInterrupt=true;
    // config.interruptTimeValue=ot-1;
    WDOG_Init(WDOG1,&config);
    return 0;
}


/*
feed dog function
*/
void xp_wdg_feed(void *arg1,void *arg2){
    WDOG_Refresh(WDOG1);
    // println("Feed dog !");
}

void WDOG1_IRQHandler(void){
    perr("watch dog will time out!!");
}

void xp_wdg_hal_stop(void){
    WDOG_Disable(WDOG1);
}

void xp_wdg_hal_open(void){
    WDOG_Enable(WDOG1);
}

#else
/******************************weak funtion****************************/
__weak int xp_wdg_mcu_init(int ot){
    println("no define function~");
}
__weak void xp_wdg_feed(void *arg1,void *arg2){
    println("no define function~");
}

__weak void xp_wdg_hal_stop(void){
    println("no define function~");
}

__weak void xp_wdg_hal_open(void){
    println("no define function~");
}

#endif

/************************************************debug****************************/
static void (*pc)(void);
/*
wdg debug
*/
static int xp_wdg_debug(char *type,char *fun,char *param){
    if(strcmp(type,"wdg")!=0)return 0;
    if(strcmp(fun,"touch")==0){
        pc=NULL;
        xp_wdg_stop();
        aos_msleep(10);
        pc();
    }

    else{
        return 0;
    }
    return 1;
}



// end of the file
